package com.pine.manager.pineframework.convertors;

import com.pine.common.beans.pineframework.request.SysUserQueryRequest;
import com.pine.common.beans.pineframework.request.SysUserSaveOrUpdateRequest;
import com.pine.common.beans.pineframework.response.SysUserQueryResponse;
import com.pine.common.convertor.EntityConvertor;
import com.pine.common.convertor.QueryConvertor;
import com.pine.common.convertor.SaveConvertor;
import com.pine.dao.pineframework.constants.GenderEnum;
import com.pine.dao.pineframework.constants.StatusEnum;
import com.pine.dao.pineframework.entity.SysUser;
import com.pine.manager.pineframework.bo.SysUserBo;
import com.pine.manager.pineframework.query.SysUserQuery;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 转换器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-12
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class SysUserConvertor implements EntityConvertor<SysUser, SysUserBo>,
        QueryConvertor<SysUserBo, SysUserQuery, SysUserQueryRequest, SysUserQueryResponse>,
        SaveConvertor<SysUserBo, SysUserSaveOrUpdateRequest> {

    @Override
    public abstract SysUser entityBoToEntity(SysUserBo sysUserBo);

    @Override
    public abstract SysUserBo entityToEntityBo(SysUser sysUser);

    @Override
    public abstract SysUserQueryResponse entityBoToQueryResponse(SysUserBo sysUserBo);

    @Override
    public abstract SysUserQuery queryRequestToQuery(SysUserQueryRequest queryRequest);


    @Override
    public abstract List<SysUser> entityBoToEntity(List<SysUserBo> entityBo);

    @Override
    public abstract List<SysUserBo> entityToEntityBo(List<SysUser> entity);

    @Override
    public abstract SysUserBo saveRequestToEntityBo(SysUserSaveOrUpdateRequest saveRequest);

    @AfterMapping
    public void afterMappingEntityBoToEntity(@MappingTarget SysUser sysUser, SysUserBo sysUserBo) {
        // entityBoToEntity() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityBoToEntity()
    }

    @AfterMapping
    public void afterMappingEntityToEntityBo(@MappingTarget SysUserBo sysUserBo, SysUser sysUser) {
        // entityToEntityBo() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityToEntityBo()
    }

    @AfterMapping
    public void afterMappingQueryRequestToQuery(@MappingTarget SysUserQuery sysUserQuery, SysUserQueryRequest sysUserQueryRequest) {
        // queryRequestToQuery() 内部其它代码执行完之后，最后会自动执行 afterMappingQueryRequestToQuery()
    }

    @AfterMapping
    public void afterMappingEntityBoToQueryResponse(@MappingTarget SysUserQueryResponse sysUserQueryResponse, SysUserBo sysUserBo) {
        // entityBoToQueryResponse() 内部其它代码执行完之后，最后会自动执行 afterMappingEntityBoToQueryResponse()
    }


    public GenderEnum integerToGenderEnum(Integer code) {
        if (Objects.isNull(code)) {
            return null;
        }
        return GenderEnum.of(code);
    }

    public Integer genderEnumToInteger(GenderEnum genderEnum) {
        if (Objects.isNull(genderEnum)) {
            return null;
        }
        return genderEnum.getCode();
    }

    public StatusEnum integerToStatusEnum(Integer code) {
        if (Objects.isNull(code)) {
            return null;
        }
        return StatusEnum.of(code);
    }

    public Integer statusEnumToInteger(StatusEnum statusEnum) {
        if (Objects.isNull(statusEnum)) {
            return null;
        }
        return statusEnum.getCode();
    }
}
